package com.oocl.im.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.hamcrest.core.IsSame;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import com.oocl.im.handler.ChatHandler;

public class ManagerServer2 implements Runnable {
	private ExecutorService threadPool;
	private LoginServer loginServer;
	private RegisterServer registerServer;
	private boolean isOpen;

	public void open() {
		threadPool = Executors.newCachedThreadPool();
		loginServer = new LoginServer(threadPool);
		registerServer = new RegisterServer(threadPool);
		threadPool.submit(loginServer);
		threadPool.submit(registerServer);
		isOpen = true;
	}

	public void close() {
		loginServer.close();
		registerServer.close();
		threadPool.shutdown();
		isOpen = false;
	}

	@Override
	public void run() {
		try {
			ServerSocket serverSocket = new ServerSocket(8500);
			while (true) {
				Socket socket = serverSocket.accept();
				handle(socket);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void handle(Socket socket) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				socket.getInputStream()));
		OutputStream out = socket.getOutputStream();
		String line = null;
		// while ((line = reader.readLine()).trim().length() != 0) {
		// System.out.println(line);
		// }
		line = reader.readLine();
		String[] headers = line.split(" ");
		if (headers[1].equals("/start")) {
			if (!isOpen) {
				open();
			}
		} else if (headers[1].equals("/stop")) {
			if (isOpen) {
				close();
			}
		}
		Element root = genHtml();
		XMLOutputter xmlOut = new XMLOutputter();
		out.write("HTTP/1.1 200 OK\r\n\r\n".getBytes());
		xmlOut.output(root, out);
		out.close();
		reader.close();
		socket.close();
	}

	public Element genHtml() {
		Integer onl = 0;
		Integer offl = 0;
		// Document dom = new Document();

		Element root = new Element("html");
		Element body = new Element("body");
		// è®¾ç½®æ ‡é¢˜
		Element divtitle = new Element("div");
		Element fonttilte = new Element("font");
		Element h1 = new Element("h1");
		h1.setText("Welcome to Server Management !");
		fonttilte.addContent(h1);
		fonttilte.setAttribute("color", "red");
		h1.setAttribute("style", "text-align:center");
		divtitle.addContent(fonttilte);
		body.addContent(divtitle);

		Element online = new Element("h3");
		Element offline = new Element("h3");
		Element onlinelist = new Element("dl");
		Element hr = new Element("hr");
		Element offlinelist = new Element("dl");
		if (isOpen) {
			for (Map.Entry<String, ChatHandler> entry : loginServer
					.getHandlerPool().entrySet()) {

				System.out.println("Key = " + entry.getKey() + ", Value = "
						+ entry.getValue());
				Element infor = new Element("dd");
				if (entry.getValue() != null) {
					onl += 1;
					infor.setText(entry.getKey() + "          online");
					onlinelist.addContent(infor);
				} else {
					offl += 1;
					infor.setText(entry.getKey() + "          offline");
					offlinelist.addContent(infor);
				}
			}
		}
		// åœ¨çº¿ç”¨æˆ·è®¾ç½®æ»šåŠ¨åŒºåŸŸ
		Element divonline = new Element("div");
		divonline.setAttribute("style", "height:250px;text-align:center");
		Element online1 = new Element("h3");
		online1.setText("Online players");
		Element onlineNumber = new Element("h4");
		onlineNumber.setText("The number of current online people is :" + onl);
		divonline.addContent(online1);
		divonline.addContent(onlineNumber);
		Element divon = new Element("div");
		divon.setAttribute("style", "overflow-y: auto;height:150px;"
				+ "text-align:center;color:green");
		divon.addContent(online);
		divonline.addContent(divon);
		body.addContent(divonline);

		// é�žåœ¨çº¿ç”¨æˆ·æ»šåŠ¨åŒºåŸŸ
		Element divoffline = new Element("div");
		divoffline.setAttribute("style", "height:250px;text-align:center");
		Element offline1 = new Element("h3");
		Element offlineNumber = new Element("h4");
		offlineNumber.setText("The number of current offline people is :"
				+ offl);
		offline1.setText("Offline players");
		divoffline.addContent(offline1);
		divoffline.addContent(offlineNumber);
		Element divoff = new Element("div");
		divoff.setAttribute("style",
				"overflow-y: auto;height:150px;text-align:center;color:gray");
		divoff.addContent(offline);
		divoffline.addContent(divoff);
		body.addContent(divoffline);

		// è®¾ç½®å�¯å�œæŒ‰é’®
		Element divbutton = new Element("div");
		Element button1 = new Element("button");
		button1.setText("start");
		button1.setAttribute("style","width:80px;height:50px");
		Element astart = new Element("a");
		astart.setAttribute("href", "start");
		astart.addContent(button1);
		Element button2 = new Element("button");
		button2.setText("stop");
		button2.setAttribute("style","width:80px;height:50px");
		Element astop = new Element("a");
		astop.setAttribute("href", "stop");
		astop.addContent(button2);
		divbutton.addContent(astart);
		divbutton.addContent(astop);
		divbutton.setAttribute("style", "text-align:center");
		online.addContent(onlinelist);
		offline.addContent(offlinelist);
		body.addContent(hr);
		body.addContent(divbutton);
		root.addContent(body);
		// dom.setRootElement(root);
		Element status = new Element("p");
		status.setText("server open?        " + isOpen);
		status.setAttribute("style", "text-align:center");
		root.addContent(status);
		return root;
	}

	public static void main(String[] args) throws Exception {
		new Thread(new ManagerServer2()).start();
	}
}
